﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myTravian
{
    public partial class traMap : Form
    {
        //string[] positions = { "北", "東", "南", "西" };
        string[] positions = { "ma_n1p7", "ma_n2p7", "ma_n3p7", "ma_n4p7" };
        System.Collections.ArrayList areaList = null;
        protected DataTable dt = null;
        /// <summary>
        /// 東
        /// </summary>
        protected HtmlElement EastURL = null;
        /// <summary>
        /// 南
        /// </summary>
        protected HtmlElement SouthURL = null;
        /// <summary>
        /// 西
        /// </summary>
        protected HtmlElement WestURL = null;
        /// <summary>
        /// 北
        /// </summary>
        protected HtmlElement NorthURL = null;

        /// <summary>
        /// 紀錄回到原點時，要知道移去那
        /// </summary>
        protected HtmlElement NextURL = null;

        public traMap()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            traBrowser1.NavigateURL(txt_url.Text);           
        }

        /// <summary>
        /// 找出地圖上有人的，沒人的不放進來
        /// </summary>
        void scanMap()
        {
            HtmlElement overlay = traBrowser1.GetElementByID("map_overlay");
            //MSHTML.IHTMLElement overlay1 = traBrowser1.getElementById("map_overlay");
            HtmlElementCollection areas = overlay.GetElementsByTagName("area");
            areaList = new System.Collections.ArrayList();
            foreach (HtmlElement area in areas)
            {
                if (area.GetAttribute("shape").ToString().ToLower().Equals("poly"))
                {
                    areaList.Add(area);
                }
            }
            //MSHTML.IHTMLElement aa = traBrowser1.getElementById("ma_n1p7");
            
            NorthURL = traBrowser1.GetElementByID("ma_n1p7");
            EastURL = traBrowser1.GetElementByID("ma_n2p7");
            SouthURL = traBrowser1.GetElementByID("ma_n3p7");
            WestURL = traBrowser1.GetElementByID("ma_n4p7");


        }


        void Navigate(string currentURL)
        {
            traBrowser1.NavigateURL(currentURL);           
        }

             
        /// <summary>
        /// 南
        /// </summary>
        void South(int loop)
        {            
            for (int i = 0; i < loop; i++)
            {
                //Navigate(SouthURL.GetAttribute("href"));
                traBrowser1.ElementInvok(SouthURL, "click");
                scanMap();
                Scan("South");                
            }               
        }

        /// <summary>
        /// 東
        /// </summary>
        void East(int loop)
        {
            for (int i = 0; i < loop; i++)
            {
                //Navigate(EastURL.GetAttribute("href"));
                traBrowser1.ElementInvok(EastURL, "click");
                scanMap();
                Scan("East");
            }  
        }

        /// <summary>
        /// 北
        /// </summary>
        void North(int loop)
        {                        
            for (int i = 0; i < loop; i++)
            {
                //Navigate(NorthURL.GetAttribute("href"));
                traBrowser1.ElementInvok(NorthURL, "click");
                scanMap();
                Scan("North");
            }
        }

        /// <summary>
        /// 西
        /// </summary>
        void West(int loop)
        {
            if (loop.Equals(0))
            {
                //Navigate(WestURL.GetAttribute("href"));//移動到新地點
                traBrowser1.ElementInvok(WestURL, "click");
                scanMap(); //取得新的地圖INFO
                Scan("West");
                NextURL = WestURL;
                
            }
            else
            {
                for (int i = 0; i < loop; i++)
                {
                    //Navigate(WestURL.GetAttribute("href"));
                    traBrowser1.ElementInvok(WestURL, "click");
                    scanMap();
                    Scan("West");
                }
                WestURL = NextURL; //Navigate(NextURL.GetAttribute("href"));
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            GetDataTable(); 
            int ieast = Convert.ToInt16(textBox1.Text) ;
            scanMap(); //找目前停留的這張圖
            //Scan("Mid");
            for (int i = 1; i <= ieast; i++)
            {
                West(0);
                North(i);
                East(i * 2);
                South(i * 2);
                West(i * 2);
                //WNES(0, WestURL);
                //WNES(i, NorthURL);
                //WNES(i * 2, EastURL);
                //WNES(i * 2, SouthURL);
                //WNES(i * 2, WestURL);
                //Navigate(NextURL.GetAttribute("href"));
            }
            this.Text = "Finish ^_^";
        }

        void Scan(string pos)
        {                      
            foreach (HtmlElement area in areaList)
            {
                if (!string.IsNullOrEmpty(area.GetAttribute("title")))
                {
                    if (!area.GetAttribute("title").ToString().Equals("未被佔領的山谷"))
                    {
                        Navigate(area.GetAttribute("href"));
                        GetVillage_info(pos);
                    }
                }
            }
            dataGridView1.DataSource = dt;
        }

        void WNES(int loop, HtmlElement arrow)
        {
            if (loop.Equals(0))
            {
                Navigate(arrow.GetAttribute("href"));//移動到新地點
                scanMap(); //取得新的地圖INFO
                //Scan("");
                NextURL = arrow;
            }
            else
            {
                for (int i = 0; i < loop; i++)
                {
                    Navigate(arrow.GetAttribute("href"));
                    scanMap();
                  //  Scan();
                }                
            }
        }

        public void GetVillage_info(string pos)
        {
            string countryName, x, y, tmp, race, association, owner, populace,xy;
            HtmlElementCollection tags ;
            HtmlElement info = traBrowser1.GetElementByID("village_info");
            if (info == null)
            {
                return;                
            }
            try
            {
                tags = info.GetElementsByTagName("div"); //<div>01.void Main</div>                
                countryName = tags.Count >0 ? tags[0].InnerText : "" ;
                tags = info.GetElementsByTagName("th");
                tmp = tags[0].InnerText.Substring(tags[0].InnerText.IndexOf('(') + 1); //(123|11)
                xy = tmp;
                x = tmp.Substring(0, tmp.IndexOf('|'));
                tmp = tmp.Replace(")", "");
                y = tmp.Substring(tmp.IndexOf('|') + 1);
                tags = info.GetElementsByTagName("td");
                race = tags[0].InnerText;
                association = tags[1].InnerText;
                owner = tags[2].InnerText;
                populace = tags[3].InnerText;

                DataRow row = dt.NewRow();
                row["countryName"] = countryName;
                row["x"] = x;
                row["y"] = y;
                row["race"] = race;
                row["association"] = association;
                row["owner"] = owner;
                row["populace"] = populace;
                row["xy"] = xy;
                row["scan_date"] = System.DateTime.Now.ToString();
                row["position"] = pos;
                dt.Rows.Add(row);
            }
            catch (Exception ex)
            {
                //System.Diagnostics.Debugger.Break();
                textBox2.Text = info.InnerText;
            }


        }

        DataTable GetDataTable()
        {
            dt = new DataTable();
            dt.Columns.Add(GetNewColumn("countryName","村莊"));
            dt.Columns.Add(GetNewColumn("x","座標X"));
            dt.Columns.Add(GetNewColumn("y", "座標Y"));
            dt.Columns.Add(GetNewColumn("race", "種族"));
            dt.Columns.Add(GetNewColumn("association", "公會"));
            dt.Columns.Add(GetNewColumn("owner", "玩家"));
            dt.Columns.Add(GetNewColumn("populace", "人口"));
            dt.Columns.Add(GetNewColumn("XY", "KEY"));
            dt.Columns.Add(GetNewColumn("scan_date", "掃描日"));
            dt.Columns.Add(GetNewColumn("position", "方位"));
            return dt;
        }

        DataColumn GetNewColumn(string colName, string colCaption)
        {
            DataColumn col = new DataColumn(colName);
            col.Caption = colCaption;
            return col;
        }

    }
}
